<template>
	<view class="container">
<!-- 		<uni-card :is-shadow="false" is-full>
			<text class="uni-h6">卡片组件通用来显示完整独立的一段信息，同时让用户理解他的作用。例如一篇文章的预览图、作者信息、时间等，卡片通常是更复杂和更详细信息的入口点。</text>
		</uni-card>
		<uni-section title="基础卡片" type="line">
			<uni-card :is-shadow="false">
				<text class="uni-body">这是一个基础卡片示例，内容较少，此示例展示了一个没有任何属性不带阴影的卡片。</text>
			</uni-card>
		</uni-section> -->
		<uni-section :title="item.title" type="line" v-for="item in page.records">
			<uni-card title="截止日期:" :sub-title="item.endTime" v-if="item.status==0" extra="进行中" @click="joinVote(item.id,item.status,item.radio)">
				<text class="uni-body">{{item.content}}</text>
			</uni-card>
			<uni-card title="截止日期:" :sub-title="item.endTime" v-if="item.status==1" extra="已取消" @click="joinVote(item.id,item.status,item.radio)">
				<text class="uni-body">{{item.content}}</text>
			</uni-card>
			<uni-card title="截止日期:" :sub-title="item.endTime" v-if="item.status==2" extra="已结束" @click="joinVote(item.id,item.status,item.radio)">
				<text class="uni-body">{{item.content}}</text>
			</uni-card>
		</uni-section>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				cover: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/094a9dc0-50c0-11eb-b680-7980c8a877b8.jpg',
				avatar: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png',
				extraIcon:{
					color: '#4cd964',
					size: '22',
					type: 'gear-filled'
				},
				currPage: 1,
				studentId:0,
				klassId:0,
				page:[]
			}
		},
		onLoad() {
			this.getdata()
		},
		// 下拉加载更多数据
		onReachBottom() {
			this.getmore();
		},
		methods: {
			onClick(e){
				console.log(e)
			},
			actionsClick(text){
				uni.showToast({
					title:text,
					icon:'none'
				})
			},
			//获取投票列表
			getdata() {
				this.studentId=localStorage.getItem("studentId");
				this.$myReq({
						url: `/manage/admin/student/getStudentById/`+this.studentId,
						method: 'get'
					})
					.then((res) => {
						this.klassId = res.data.klassId
						console.log("klassId="+this.klassId)
						this.$myReq({
								url: `/vote/api/getVotes/` + this.klassId+`/`+this.currPage ,
								method: 'get'
							})
							.then((res) => {
								console.log(res.data)
								this.page = res.data
							})
							.catch((err) => {
								console.log(err)
							})
					})
					.catch((err) => {
						console.log(err)
					})
			},
			//下拉加载更多
			getmore() {
				this.currPage++
				this.$myReq({
					url: `/vote/api/getVotes/` + this.klassId+`/`+this.currPage ,
					method: 'get'
				}).then(r => {
					if (!(r.data.records.length == 0 || r.data.records.length == null)) {
						for (let i of r.data.records) {
							console.log("page为:"+this.page)
							this.page.records.push(i)
						}
					} else {
						this.showToast("没有更多内容")
					}
			
				})
			},
			//没有更多提示
			showToast(text) {
				uni.showToast({
					title: text,
					icon: "none"
				});
			},
			//参与投票
			joinVote(id,status,radio){
				if(status==0){
					//判断缓存或数据库中是否存在该学生的该条投票,若存在则跳转投票数明示,若无则跳转投票页
					this.$myReq({
						url: `/vote/api/hasVoting/` + this.studentId+`/`+id ,
						method: 'get'
					}).then(r => {
						console.log(r)
						if(r.code==200){
							uni.navigateTo({
								url: '/pages/vote/voteDetails?id='+id+"&radio="+radio
							});
						}else if(r.code==119){
							uni.navigateTo({
								url: '/pages/vote/hasVoteDetails?id='+id+"&radio="+radio
							});
						}
								
					})
					
				}else if(status==1){
					uni.showToast({
						title: "投票已取消",
						icon: "none"
					});
				}else if(status==2){
					//直接跳转投票数明示
					uni.navigateTo({
						url: '/pages/vote/hasVoteDetails?id='+id+"&radio="+radio
					});
				}else{
					uni.showToast({
						title: "数据异常",
						icon: "none"
					});
				}
			}
		}
	}
</script>


<style>
</style>